package com.ujs.edu.medicine.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.ujs.edu.medicine.dao.*;
import com.ujs.edu.medicine.dataobject.*;
import com.ujs.edu.medicine.util.Result;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:
 * @Date: Created in 22:18 2020/8/27
 * Description: 评论控制类
 * Modified By:
 */
@RestController
public class CommentController {

    @Autowired
    YunjiayiCommentMapper yunjiayiCommentMapper;
    @Autowired
    YunjiayiUserMapper yunjiayiUserMapper;
    @Autowired
    YunjiayiLikeMapper yunjiayiLikeMapper;
    @Autowired
    YunjiayiPostMapper yunjiayiPostMapper;
    @Autowired
    TablePostMesMapper tablePostMesMapper;
    @Autowired
    LiketableCommentUserMapper liketableCommentUserMapper;

    @Autowired
    HttpServletRequest request;
    public String gettoken(){
        return request.getHeader("Authorization");
    }


    /*增加评论*/
    @RequestMapping(value = "/api/comment",method = RequestMethod.POST)
    public Result addComment(@RequestBody JSONObject object){
        Result result=new Result();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid= JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("没有登陆，请登陆");
            return result;
        }

        YunjiayiComment comment=new YunjiayiComment();
        TablePostMes tablePostMes=new TablePostMes();

        YunjiayiPost post=yunjiayiPostMapper.selectByPrimaryKey(object.getInteger("postId"));
        YunjiayiUser user=yunjiayiUserMapper.selectByPrimaryKey(myuserid);

        comment.setContent(object.getString("content"));
        comment.setAuthorId(myuserid);
        comment.setPostId(object.getInteger("postId"));
        if (object.getInteger("parentCommentID")==null){
            int addcom=yunjiayiCommentMapper.insert(comment);
            if (addcom!=0){
                tablePostMes.setFromuserid(myuserid);
                tablePostMes.setTouserid(post.getUserid());
                String content="用户"+user.getUsername()+"评论了你的"+post.getContent()+"帖子";
                tablePostMes.setContent(content);
                tablePostMes.setPostid(object.getInteger("postId"));
                int addposmes=tablePostMesMapper.insert(tablePostMes);
                if (addposmes!=0){
                    result.setCode(0);
                    result.setMessage("评论成功");
                }else {
                    result.setCode(1);
                    result.setMessage("发送帖子评论通知失败");
                }
            }else {
                result.setCode(1);
                result.setMessage("添加评论失败");
            }
        }else {
            comment.setParentid(object.getInteger("parentCommentID"));
            int addcom=yunjiayiCommentMapper.insert(comment);
            if (addcom!=0){
                tablePostMes.setFromuserid(myuserid);
                System.out.println(post.getUserid());
                tablePostMes.setTouserid(post.getUserid());
                String content="用户"+user.getUsername()+"评论了你的"+post.getContent()+"帖子";
                tablePostMes.setContent(content);
                tablePostMes.setPostid(object.getInteger("postId"));
                int addposmes=tablePostMesMapper.insert(tablePostMes);

                YunjiayiComment comment1=yunjiayiCommentMapper.selectByPrimaryKey(object.getInteger("parentCommentID"));

                String content1="用户"+comment1.getAuthorId()+"回复了你的评论";
                tablePostMes.setContent(content1);
                tablePostMes.setFromuserid(myuserid);
                tablePostMes.setTouserid(comment1.getAuthorId());
                int addpostmestoparent=tablePostMesMapper.insert(tablePostMes);
                if (addposmes!=0&&addpostmestoparent!=0){
                    result.setCode(0);
                    result.setMessage("评论成功");
                }else {
                    result.setCode(1);
                    result.setMessage("发送帖子评论通知失败");
                }
            }else {
                result.setCode(1);
                result.setMessage("添加评论失败");
            }
        }

        return result;
    }


    /*获取帖子评论*/
    @RequestMapping("/api/comment/list")
    public Result getComments(@RequestParam("postId")Integer postId) {
        Result result=new Result();
        JSONObject object=new JSONObject();

        String token=gettoken();
        int myuserid=0;
        if(token!=null){

            try {
                myuserid=JWT.decode(token).getClaim("id").asInt();
            }catch (Exception e){
                result.setCode(1);
                result.setMessage("您还没有登陆，请登陆");
                return result;
            }
        }


        List<YunjiayiComment>comments=yunjiayiCommentMapper.getCommentsByPostId(postId);

        Author author=null;
        List<LiketableCommentUser> liketableCommentUsers=new ArrayList<>();

        try {
            for (YunjiayiComment comment:comments){
                author=yunjiayiUserMapper.selectPartInfo(comment.getAuthorId());
                comment.setAuthor(author);

                liketableCommentUsers=liketableCommentUserMapper.selectBycommentId(comment.getCommentId());

                comment.setLike_num(liketableCommentUsers.size());
                if (liketableCommentUsers.size()==0){
                    comment.setIsLike(false);
                }else {
                    for (LiketableCommentUser liketableCommentUser:liketableCommentUsers){
                        if (liketableCommentUser.getUserid()==myuserid){
                            comment.setIsLike(true);
                        }else{
                            comment.setIsLike(false);
                        }
                    }

                }

                if (comment.getParentid()!=null){
                    YunjiayiComment comment1=yunjiayiCommentMapper.selectByPrimaryKey(comment.getParentid());
                    author=yunjiayiUserMapper.selectPartInfo(comment1.getAuthorId());
                    comment.setParentCommentRealname(author.getRealname());
                }

            }

            object.put("comment_num",Integer.toString(comments.size()));
            object.put("data",comments);

            result.setCode(0);
            result.setMessage("查询成功");
            result.setData(object);
        }catch (Exception e){
            result.setCode(0);
            result.setMessage("查询失败");
        }

        return result;
    }



    /*点赞取消点赞*/
    @RequestMapping("/api/comment/like")
    public Result postLike(@RequestBody JSONObject object) {
        Result result = new Result();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid=JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            e.printStackTrace();
        }


        List<LiketableCommentUser> liketableCommentUsers=liketableCommentUserMapper.selectBycommentId(object.getInteger("comment_id"));
        LiketableCommentUser like=null;

        for (LiketableCommentUser liketableCommentUser:liketableCommentUsers){
            if (liketableCommentUser.getUserid().equals(myuserid)){
                like=liketableCommentUser;
            }
        }

        if (like != null) {
            int delcom=liketableCommentUserMapper.deleteByPrimaryKey(like.getLiketableid());

            if (delcom!=0){
                result.setCode(0);
                result.setMessage("取消点赞成功");
            }else {
                result.setCode(1);
                result.setMessage("取消点赞失败");
            }
            return result;
        }

        like=new LiketableCommentUser();
        like.setCommentid(object.getInteger("comment_id"));
        like.setUserid(myuserid);
        int li = liketableCommentUserMapper.insert(like);
        if (li == 1) {
            result.setCode(0);
            result.setMessage("点赞成功");
        } else {
            result.setCode(1);
            result.setMessage("点赞失败");
        }

        return result;
    }

}
